﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HmFramework.Components;
using HmFramework.Components.TreeList;
using HmFramework.UI.Utils;
using HmFramework.Data;
using HmFramework.Log;
using CuteSprite.Eme.UI.Utils;
using CuteSprite.Eme.Data.BsdUserCenter;

namespace CuteSprite.Eme.Server.Configuration
{
	public partial class FrmOrganizeDataTransform : HmBaseForm
	{
		private EntityList<BsdOrganize> _OrganizeEntities = null;

		public EntityList<BsdOrganize> OrganizeEntities
		{
			get
			{
				if (_OrganizeEntities != null) { return _OrganizeEntities; }
				_OrganizeEntities = BsdOrganize.FindAll();
				return _OrganizeEntities;
			}
		}

		public FrmOrganizeDataTransform()
		{
			InitializeComponent();
			InitForm();
		}

		protected override void InitComponent()
		{
			base.InitComponent();

			tvwSrc.NodesConnector = null;
			tvwSrc.ExpandButtonType = eExpandButtonType.Triangle;
			tvwSrc.ExpandWidth = 14;
			tvwDest.NodesConnector = null;
			tvwDest.ExpandButtonType = eExpandButtonType.Triangle;
			tvwDest.ExpandWidth = 14;
		}

		public override void FormOnLoad()
		{
			base.FormOnLoad();

			LoadOrganizeEntities(tvwSrc.Nodes, null);
			LoadOrganizeEntities(tvwDest.Nodes, null, false);
		}

		private void LoadOrganizeEntities(NodeCollection nodes, BsdOrganize parent, Boolean isSrc = true)
		{
			if (OrganizeEntities == null) { return; }
			var entities = OrganizeEntities.FindAll(EntityHelper.FieldParentID, parent != null ? parent.ID : 0);
			if (entities != null && entities.Count > 0)
			{
				//String orderClause = sort != null ? MakeOrder(sort.ColumnName) : null;
				entities.Sort(BsdOrganize.__.Sort, false);
				for (int i = 0; i < entities.Count; i++)
				{
					var entity = entities[i];
					var node = CreateOrganizeNode(entity);
					nodes.Add(node);
					// 递归，载入子节点
					LoadOrganizeEntities(node.Nodes, entity, isSrc);
				}
			}
		}

		private Node CreateOrganizeNode(BsdOrganize organize, Boolean isSrc = true)
		{
			var node = new Node(organize.FullName);
			if (isSrc)
			{
				node.Name = "nodOrganizeS{0}".FormatWith(organize.ID);
			}
			else
			{
				node.Name = "nodOrganizeD{0}".FormatWith(organize.ID);
			}
			node.Tag = organize;
			node.Expanded = true;
			return node;
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void btnTransform_Click(object sender, EventArgs e)
		{
			if (tvwSrc.SelectedNode == null)
			{
				MessageBoxEx.Show("请选择源部门！", "数据迁移", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			if (tvwDest.SelectedNode == null)
			{
				MessageBoxEx.Show("请选择目标部门！", "数据迁移", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			var srco = tvwSrc.SelectedNode.Tag as BsdOrganize;
			_SrcID = srco.ID;
			var desto = tvwDest.SelectedNode.Tag as BsdOrganize;
			_DestID = desto.ID;

			HmTrace.WriteInfo("所属部门:{0} 下的数据迁移到部门:{1} ......", srco.FullName, desto.FullName);

			btnTransform.Enabled = false;
			btnClose.Enabled = false;
			if (!backgroundWorker1.IsBusy) { backgroundWorker1.RunWorkerAsync(); }
		}

		private Int32 _SrcID = 0;
		private Int32 _DestID = 0;
		private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
		{
			try
			{
				BsdOrganize.Transform(_SrcID, _DestID);
			}
			catch (Exception ex)
			{
				ProcessException(ex);
			}
		}

		private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			this.BeginInvoke(new MethodInvoker(() =>
			{
				MessageBoxEx.Show("部门数据迁移成功！", "数据迁移", MessageBoxButtons.OK, MessageBoxIcon.Information);
				btnTransform.Enabled = true;
				btnClose.Enabled = true;
			}));
		}
	}
}
